package com.yunzheng.studyLeaveSystem.filter;



import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: YunZheng
 * @Date: 2023/03/20/20:02
 * @Description:
 */
@WebFilter(value = {},filterName = "authFilter")
public class AuthFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("--------登录拦截器--start----------");
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpSession session = request.getSession();
        Object id = session.getAttribute("id");
        if (id!= null) {
            //说明用户登录了
            filterChain.doFilter(servletRequest, servletResponse);
        }
        else {
            //说明用户未登录
            //跳转到登陆页面
            request.getRequestDispatcher("").forward(request,response);
        }
        System.out.println("--------登录拦截器--end----------");
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }
}

